﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer.App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_BreatheWater : Buff, IConsumable
    {
        public Spell_BreatheWater() 
        {
            minutesLeft = 10;
        }

        public bool MeetsConsumeRequirements(Character character, Loot loot)
        {
            return true;
        }

        public void Consume(Character myCharacter, Loot lootitem)
        {
            TargetCreatureBase = myCharacter;

            minutesLeft = 20;

            LockItem lockItem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
            lock (lockItem.LockObj)
            {
                Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

                foreach (MapItem mapitem in CurrentMap.MapItems)
                {
                    if (Math.Abs(mapitem.MapX - myCharacter.MapX) <= 8 && Math.Abs(mapitem.MapY - myCharacter.MapY) <= 8)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase is Character)
                            {
                                Character character = (Character)creaturebase;
                                character.PlaySound("DrinkBottle", myCharacter.MapX, myCharacter.MapY);
                                character.AddCreatureBase(myCharacter);
                            }
                        }
                    }
                }

                SpellIcon potion = new SpellIcon();
                foreach (KeyValuePair<int, SpellIcon> value in TargetCreatureBase.GameHandler.Spells)
                {
                    if (value.Value.Name == "Breathe Water")
                    {
                        potion = value.Value.clone();
                        break;
                    }
                }

                this.SpellID = TargetCreatureBase.GameHandler.ItemIDGen.GetSpellID();
                this.spellIcon = potion;

                //Remove existing recall potions
                foreach (Buff buff in TargetCreatureBase.buffManager.Buffs)
                {
                    if (buff.spellIcon.Name == base.spellIcon.Name)
                    {
                        buff.RemoveSpell(false, false);
                    }
                }
                TargetCreatureBase.buffManager.AddBuff(this);
                TargetCreatureBase.buffManager.UpdateBuffs();


                spellTimer.Start();
            }
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;

            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);
            
            spellTimer.Start();
        }
        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            myCreatureBase.Update_Character();
            TargetCreatureBase.buffManager.UpdateBuffs();

            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 8 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character character = (Character)creaturebase;
                            character.PlaySound("Bubbles", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                        }
                    }
                }
            }

            //Start Spell Timer
            spellTimer.Start();
        }
    }
}
